// pages/my/setting/index.js
const App = getApp()
const AJAX = require("../../../common/request")
const API = require("../../../common/api")
const common = require("../../../common/common")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navTop: 0,
        navContentHeight: 0,
        placeholderHeight: 0,
        userPhone: '',
        nickname: '23',
        avatarUrl: '',
        method: 'get',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            navTop: App.globalData.navTop,
            navContentHeight: App.globalData.navContentHeight,
            nickname:  App.globalData.user.wxLoginUsername,
            avatarUrl: App.globalData.user.wxLoginImg,
            userPhone: App.globalData.user.wxLoginPhone,
            method: options.method ? options.method : 'get'
        })
        const query = wx.createSelectorQuery();
        query.select('.head').boundingClientRect();
        query.exec((res) => {
            this.setData({
                placeholderHeight: res[0].height
            })
        });

    },
    onShow() {
   
        if (App.globalData.user.wxLoginPhone == "" && App.globalData.user.wxLoginPhone == null) {
            wx.showToast({
                title: '尚未登录',
                icon: "none"
            })
            wx.navigateTo({
                url: '/pages/login/index',
            })
        }
    },
    nicknameInput(e){
        this.setData({
            nickname:e.detail.value
        })
    },

    updateBWechatUser() {
        if (this.data.method == 'get') {
            this.setData({
                method: "update"
            })
        } else {
            AJAX.POST('/system/AppWechat/updatewechatuser', {
                wechatUserId: App.globalData.user.wechatUserId,
                wechatUserName: this.data.nickname,
                phone: this.data.userPhone,
                headImg: this.data.avatarUrl
            }).then((res) => {
                console.log(res)
                wx.setStorageSync('wxLoginUsername', this.data.nickname)
                wx.setStorageSync('wxLoginImg',this.data.avatarUrl)
                wx.setStorageSync('wxLoginPhone',this.data.userPhone)
                App.globalData.user.wxLoginUsername = this.data.nickname
                App.globalData.user.wxLoginImg = this.data.avatarUrl
                App.globalData.user.wxLoginPhone = this.data.userPhone

                wx.navigateBack()
            })
        }
    },
    //上传头像
    onChooseAvatar(e) {
        // 执行头像上传
        AJAX.UPLOADFILE("/system/AppWechat/addfile", e.detail.avatarUrl, {
            file: e.detail.avatarUrl
        }).then((res) => {
            let data = res.data
            if (typeof res.data === 'string') {
                data = JSON.parse(res.data)
            }
            console.log(data)
            // 图片地址赋值回显
            let imgUrl = API.UPLOADURL + data.msg
            console.log(imgUrl)
            console.log("图片地址赋值回显", imgUrl)
            this.setData({
                avatarUrl: imgUrl
            })
        })
    },
    goBack() {
        const pages = getCurrentPages();
        //获取不到上级返回首页
        if (pages.length > 1) {
            wx.navigateBack({})
        } else {
            wx.reLaunch({
                url: '/pages/index/index',
            })
        }
    },

    getPhoneNumber: common.throttle(function (e) {
        AJAX.GET("/wxLogin/getPhone", {
            openId: wx.getStorageSync('wxLoginOpenid'),
            code: e.detail.code
        }, true).then(res => {
            console.log(res)
            this.setData({
                userPhone: res.phone,
            })

        }).catch(err => {
            console.error(err)
            wx.showToast({
                title: '获取失败',
                icon: "none"
            })
        })
    }, 3000),
    onShareAppMessage(options) {
		// 默认分享配置
		return {
			title: App.globalData.onShareAppMessage.title,
			path: App.globalData.onShareAppMessage.path,
			imageUrl: App.globalData.onShareAppMessage.imageUrl,
		}
	},
})